<?php
require_once "checklogin.php";
require_once "../lib/MySQLDB.class.php";
require_once "../lib/Page.class.php";
$pageSize=3;
$page=isset($_GET["page"])?$_GET["page"]:1;
$keWoreds=isset($_GET["keyWord"])?$_GET["keyWord"]:'';
$sqlCnt="select count(*) cnt from newsinfo";
$sqlQuery="select newsID,newsTitle,newsCID,cName,newsViews,newsDate from newsinfo join  category
on newsinfo.newsCID=category.id";
if(!empty($keWoreds)){
    $arrkey=explode('',$keWoreds);
    $sqlCnt.=" where";
    $sqlQuery.=" where";
    foreach ($arrkey as $v){
        $sqlCnt.=" newsTitle like '%{$v}%' or";
        $sqlQuery.=" newsTitle like '%{$v}%' or";
    }
    $sqlCnt=rtrim($sqlCnt,"or");
    $sqlQuery=rtrim($sqlQuery,"or");
}
$db=MySQLDB::getInstance();
$row=$db->fetchRow($sqlCnt);
$total=$row['cnt'];
$maxPage=ceil($total/$pageSize);
$page=$page>$maxPage?$maxPage:$page;
$page=$page<1?1:$page;
$url="?page={page}";
if($keWoreds){
    $url.="&keyWord=".$keWoreds;
}
$pageHtml="";
if($total>$pageSize){
    $pageObj=new Page($total,$pageSize,$page,$url,2);
    $pageHtml=$pageObj->getPageHtml();
}
$sqlQuery.=" order by newsDate desc,newsID desc LIMIT " . ($page-1) * $pageSize . ",$pageSize;";
$dataArr=$db->fetchAll($sqlQuery);
require_once "view/newsList.html";